<template>
  <page-wrap ref="pageWrap" hideTab back title="职途顾问" :showTop="false" :dark="loaded ? false : true"
    :hideSafeArea="true">
    <view class="buy-page">
      <scroll-view class="container" scroll-y="true">
        <view class="sale-introcution"></view>
      </scroll-view>
      <view class="operation">
        <view class="left-button" @click="gotoBook">体验咨询{{ leftAskTime }}次</view>
        <view class="right-button" @click="gotoPay">
          <span style="margin-right: 16rpx">立即购买</span>
          <span>￥49.9</span>
        </view>
      </view>
    </view>
  </page-wrap>
</template>

<script>
import { env } from "@/api/root";
import {
  addCartAndToOrder,
  addZhituPayOrder,
  getVasRights,
} from "@/api/modules/career";

export default {
  components: {},
  data() {
    return {
      navHeight: getApp().globalData.navHeight,
      leftAskTime: 1,
      provider: {},
      mallProductId: "",
      rightProductId: "",
      isConsultant: false,
    };
  },
  onLoad(opt) {
    if (opt.isConsultant == "true") {
      this.isConsultant = true;
    } else {
      this.isConsultant = false;
    }
  },
  onShow() {
    // this.getUserInfoFn();
  },
  onHide() {},
  created() {
    // uni.showLoading({
    //   title: "获取服务信息中...",
    //   mask: true,
    // });
    this.getVasRights();
    //获取支付提供商
    uni.getProvider({
      service: "payment",
      success: (res) => {
        this.provider = res.provider;
        console.log("this.provider", this.provider);
      },
    });
  },
  mounted() {
    // this.getConsultantList(20);
  },
  methods: {
    async getVasRights() {
      const params = {
        cardType: uni.getStorageSync("cardType"),
        personId: uni.getStorageSync("person-id").slice(2) || "",
      };
      const res = await getVasRights(params);
      if (res.code == 200) {
        console.log("res.data", res.data);
        this.mallProductId = res.data.CONSULTE_SERVICE.mallProductId;
        this.rightProductId = res.data.CONSULTE_SERVICE.id;
        this.leftAskTime = res.data.CONSULTE_SERVICE.experience[0].leftValue;
      }
    },
    gotoBook() {
      if (!this.isConsultant) {
        if (this.leftAskTime > 0) {
          if (uni.getStorageSync("isBuy") == 1) {
            uni.setStorageSync("edition-Type", 10);
          } else {
            uni.setStorageSync("edition-Type", 20);
          }
          uni.navigateTo({
            url: `/pagesSub/userCenter/careerPathV2/components/BookConsultant`,
          });
        } else {
          uni.showToast({
            icon: "none",
            title: "您的体验次数已用完，如需使用，请先购买~",
            duration: 1000,
          });
        }
      }
    },
    gotoPay() {
      if (!this.isConsultant) {
        console.log("这里是去支付");
        this.addCartAndToOrder();
      }
    },
    //1.生成订单
    async addCartAndToOrder() {
      uni.showLoading({
        title: "生成订单中...",
        mask: true,
      });
      const params = {
        orderType: "ZT_SERVICE",
        mallProductId: this.mallProductId, // 这个等着后端返回
        channelType: 80,
        sourceType: 3,
        personId: uni.getStorageSync("person-id").slice(2) || undefined,
        cardType: uni.getStorageSync("cardType"),
        orderCondition: {
          rightProductId: this.rightProductId, // 等着后端返回
        },
      };
      const res = await addCartAndToOrder(params);
      if (res.code == 200) {
        console.log("res");
        this.addZhituPayOrder(res.data);
      } else {
        uni.hideLoading();
        uni.showToast({
          title: res.status_desc || "生成订单失败",
          icon: "none",
          duration: 2000,
        });
      }
    },
    //2.通过订单获取支付信息
    async addZhituPayOrder(data) {
      const params = {
        ...data,
        orderType: "ZT_SERVICE",
        wayCode: "WX_LITE",
        personId: uni.getStorageSync("person-id").slice(2) || undefined,
        personName: uni.getStorageSync("personName"),
        openid: uni.getStorageSync("openid"),
      };
      const res = await addZhituPayOrder(params);
      if (res.code == 200) {
        this.requestPayment(res.data);
      } else {
        uni.hideLoading();
        uni.showToast({
          title: res.status_desc || "获取商品信息失败",
          icon: "none",
          duration: 2000,
        });
      }
    },
    //3.使用支付信息调用支付
    requestPayment(data) {
      uni.hideLoading();
      if (this.provider.indexOf("wxpay") < 0) {
        return;
      }
      const payData = JSON.parse(data.payData);
      uni.requestPayment({
        provider: this.provider[0],
        timeStamp: payData.timeStamp,
        nonceStr: payData.nonceStr,
        package: payData.package,
        signType: payData.signType,
        paySign: payData.paySign,
        success: (res) => {
          console.log("支付成功");
          uni.setStorageSync("edition-Type", 10);
          uni.setStorageSync("isBuy", 1);
          // this.handlePaySuccess(res);
          uni.redirectTo({
            url:
              `/pagesSub/userCenter/careerPathV2/components/zhituPayFinish?mallOrderId=` +
              data.orderId,
          });
        },
        fail: (err) => {
          uni.showToast({
            title: "支付失败,请您重新支付",
            icon: "none",
            duration: 3000,
          });
          console.log("支付失败");
          // uni.navigateTo({
          //   url:
          //     `/pagesSub/userCenter/careerPathV2/components/PayFinish?isFailed=` +
          //     true,
          // });
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
$img-base-person: $img-base + "careerPath/";
.buy-page {
  width: 100%;
  .container {
    height: 90vh;
    .sale-introcution {
      height: 4360rpx;
      background-image: url($img-base-person + "BuyPageBackground2x.png");
      background-size: 100%;
    }
  }

  .operation {
    width: 100%;
    height: 10vh;
    position: absolute;
    bottom: 0;
    display: flex;
    flex-direction: row;
    padding: 18rpx 32rpx 30rpx 30rpx;
    background-color: #ffffff;
    .left-button {
      flex-basis: 240rpx;
      height: 80rpx;
      border-radius: 200rpx;
      border: 2rpx solid #ff711e;
      margin-right: 16rpx;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      color: #ff711e;
      font-size: 30rpx;
    }
    .right-button {
      flex-grow: 1;
      height: 80rpx;
      background-image: url($img-base + "careerPath/yxBuyButton.png");
      background-size: 100% 100%;
      color: #ffffff;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: left;
      padding: 24rpx 48rpx 20rpx 48rpx;
      span {
        font-family: D-DIN-PRO, D-DIN-PRO;
        font-weight: 800;
        font-size: 32rpx;
      }
    }
  }
}
</style>
